#ifndef _MAPMERGE_H
#define _MAPMERGE_H
#include <string>
#include <map>
#include <numeric>
#include <functional>
#include <iostream>
struct plus2nd {
    template <typename T1, typename T2>
    inline T1 operator()(T1 t1, const T2 &t2) const {
        return t1 + t2.second;
    }
};

template <typename Map, typename Key, typename Value>
Value accumulate_range(const Map & map, const Key & key, Value init)
{
    typedef typename Map::const_iterator const_iterator;
    typedef typename Map::value_type value_type;
    std::pair<const_iterator, const_iterator> range = map.equal_range(key);
    return std::accumulate(range.first, range.second, init, plus2nd());
}

#endif //_MAPMERGE_H